import 'package:flutter/cupertino.dart';
import 'package:life_service/color/hex.dart';

class DrawUnderLine extends StatefulWidget {
  final Widget child;

  final Color? color;

  final double dx;

  final double dy;

  final double strokeWidth;

  const DrawUnderLine(
      {super.key,
      required this.child,
      this.color,
      this.dx = 0.0,
      this.dy = 0.0,
      this.strokeWidth = 1.0});

  @override
  State<DrawUnderLine> createState() => _DrawUnderLineState();
}

class _DrawUnderLineState extends State<DrawUnderLine> {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _DrawLineColorPainter(
          color: widget.color ?? HexColor(HexColor.colorF5f5),
          dx: widget.dx,
          dy: widget.dy,
          strokeWidth: widget.strokeWidth),
      child: widget.child,
    );
  }
}

class _DrawLineColorPainter extends CustomPainter {
  final Color color;
  final double dx;
  final double dy;
  final double strokeWidth;
  _DrawLineColorPainter(
      {required this.color,
      this.dx = 0.0,
      this.dy = 0.0,
      this.strokeWidth = 1.0});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..color = color
      ..strokeWidth = strokeWidth;
    canvas.drawLine(Offset(dx, size.height - dy),
        Offset(size.width - dx, size.height - dy), paint);
  }

  @override
  bool shouldRepaint(covariant _DrawLineColorPainter oldDelegate) =>
      this != oldDelegate;
}
